package org.zheng.shop.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.zheng.shop.entity.Company;
import org.zheng.shop.repository.CompanyRepository;

import java.util.List;

@Service
public class CompanyService {
    
    @Autowired
    private CompanyRepository companyRepository;
    
    public Company getCompanyInfo() {
        List<Company> companies = companyRepository.findAll();
        if (companies.isEmpty()) {
            // 如果没有公司信息记录，创建一个空的默认记录
            Company defaultCompany = new Company();
            defaultCompany.setCompanyName("默认公司名称");
            return companyRepository.save(defaultCompany);
        }
        return companies.get(0); // 返回第一条记录，因为公司信息只有一条
    }
    
    @Transactional
    public Company updateCompanyInfo(Company company) {
        List<Company> companies = companyRepository.findAll();
        if (!companies.isEmpty()) {
            // 如果存在公司记录，使用现有的ID
            company.setId(companies.get(0).getId());
        }
        return companyRepository.save(company);
    }
} 
 